#ifndef _marquee_dragonball_
#define _marquee_dragonball_

#include "marquee.h"
#include "igslib_iserial_manager.h"
#include "protocol_DragonBall.h"

namespace igslib
{
    class marquee_Grgn:public CMarquee
    { 
		protected:
			static int com_number;
			static int PWM_ID;
			static int Marquee_ID;
			int LocPWM_ID;
			int LocMarquee_ID;
			char setup_ctrl[2]={0};
			char setup_marquee_ctrl[5]={0};
        public:

		  marquee_Grgn()
		  {
		    com_number++;
			LocPWM_ID=0;
			LocMarquee_ID=0;
		    spi = nullptr;
			setup_ctrl[0]=1;
			setup_marquee_ctrl[0]=1;			
		  }

          ~marquee_Grgn()
		 {
			if(com_number>-1)
			com_number--;
			
			if(LocPWM_ID>0)
			PWM_ID--;
			
			if(LocMarquee_ID>0)
			LocMarquee_ID--; 
		 }

		  void SetSPI(ISpi* value) { spi = value; }		

         void RunGroup(int value,bool value1)
		 {
			
			char dtmp[3]={0};
			gpno=value;
			dtmp[0]=gpno;
			dtmp[1]=bstime;			
		    SR=value1;
			if(SR==true)
				dtmp[2]=1;
			else			
				dtmp[2]=0;
			Protocol_DragonBall* tmp = (Protocol_DragonBall*)(spi);
			tmp-> SetMarquee(LocMarquee_ID,dtmp);	 
		 }

         void ImportGroup(int gno,int pcnt,unsigned char* data)
		 {
		 	int arry_size=pcnt+2;
			char dtemp[arry_size];
			dtemp[0]=gno;
			dtemp[1]=pcnt;	
			for(int i=0;i<pcnt;i++)
			{
				dtemp[i+2]=data[i];
			}	
			Protocol_DragonBall* tmp = (Protocol_DragonBall*)(spi);
			tmp-> LoadGroup(LocMarquee_ID,arry_size,dtemp);	
		 }

         void ImportPattern(int pno,int ledcnt,const LEDColor* data)
		 {	 
			int arry_size=ledcnt*3+2;
			char dtemp[arry_size];
			dtemp[0]=pno;
			dtemp[1]=ledcnt;	
			for(int i=0;i<ledcnt;i++)
			{
				dtemp[i*3+2]=data->Red;
				dtemp[i*3+3]=data->Green;
				dtemp[i*3+4]=data->Blue;
				data++;		
			}	
			Protocol_DragonBall* tmp = (Protocol_DragonBall*)(spi);
			tmp-> LoadPattern(LocMarquee_ID,arry_size,dtemp);	 
		 }

         void SetPatternDuration(const float& value)
		 {
			char dtmp[3]={0};
			dtmp[0]=gpno;
			bstime=value;
			dtmp[1]=bstime;			
			if(SR==true)
				dtmp[2]=1;
			else			
				dtmp[2]=0;
			Protocol_DragonBall* tmp = (Protocol_DragonBall*)(spi);
			tmp-> SetMarquee(LocMarquee_ID,dtmp);	 
		 
		 }
	  

		void SetGPIO(IGPIO* value,IGPIO* value1,IGPIO* value2)	
		{
			gpclk = value;
			gpdata = value1; 
			gplatch= value2; 			
			
			if(!gpclk )
			throw_igslib_exception("marquee GPIO is NULL");	
			if(!gpdata )
			throw_igslib_exception("marquee GPIO is NULL");	
			
			
			if((PWM_ID+Marquee_ID)<com_number)  //判定是否有設超過
			{
				LocMarquee_ID=Marquee_ID;
				Marquee_ID++;
			}
			
			setup_marquee_ctrl[1]=(char)gpclk->GetNumber();
			setup_marquee_ctrl[2]=(char)gpdata->GetNumber();
			
			if(gplatch)
			setup_marquee_ctrl[3]=(char)gplatch->GetNumber();
			
			Protocol_DragonBall* tmp = (Protocol_DragonBall*)(spi);
			tmp-> SetupMarquee(LocMarquee_ID,setup_marquee_ctrl);
			
			
		}
			
		 void SetGPIO(IGPIO* value) 
		{ 
			gplamp = value; 
			if(!gplamp )
			throw_igslib_exception("marquee_pwm GPIO is NULL");	

			if((PWM_ID+Marquee_ID)<com_number)  //判定是否有設超過
			{
				LocPWM_ID=PWM_ID;
				PWM_ID++;
			}		
			setup_ctrl[1]=(char)gplamp->GetNumber();
			Protocol_DragonBall* tmp = (Protocol_DragonBall*)(spi);
			tmp-> SetupPWM(LocPWM_ID,setup_ctrl);

		}

		void SetDutyCycle(int value)
		{
			duty=value;
			Protocol_DragonBall* tmp = (Protocol_DragonBall*)(spi);
			tmp-> SetPWM(LocPWM_ID,duty);
		}
		
		 	
    };
	int marquee_Grgn::com_number = 0;
	int marquee_Grgn::PWM_ID = 0;
	int marquee_Grgn::Marquee_ID = 0;
};

#endif
